<template>
  <div class="select-page">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back-btn" @click="goBack">
        <i class="arrow-left">←</i>
      </div>
      <div class="title">选择患者</div>
    </div>

    <!-- 搜索框 -->
    <div class="search-box">
      <input
        type="text"
        v-model="searchKeyword"
        placeholder="请输入患者姓名相关信息"
        class="search-input"
        @input="handleSearch"
      >
    </div>

    <!-- 病症分组列表 -->
    <div class="disease-list" v-if="!loading">
      <!-- 所有患者选项 -->
      <div class="group-item">
        <div class="group-header" @click="selectPatients('all')">
          <div class="group-name">所有患者</div>
          <div class="patient-count">{{ totalPatients }}人</div>
          <i class="arrow-right">></i>
        </div>
      </div>

      <!-- 病症分组 -->
      <div class="group-item" v-for="(group, index) in groupList" :key="index">
        <div class="group-header" @click="selectPatients(group.id)">
          <div class="group-name">{{ group.groupName }}</div>
          <div class="patient-count">{{ group.patientList ? group.patientList.length : 0 }}人</div>
          <i class="arrow-right">></i>
        </div>
      </div>
    </div>

    <div v-else class="loading">
      加载中...
    </div>

    <!-- 底部已选择计数 -->
    <div class="bottom-bar">
      <div class="selected-count">已选择{{ selectedCount }}人</div>
      <div class="confirm-btn" @click="confirmSelection">确定</div>
    </div>
  </div>
</template>

<script>
import { selectGroup, selectPatient } from "@/api/message/message";

export default {
  name: 'SelectDisease',
  data() {
    return {
      searchKeyword: '',
      groupList: [],
      selectedCount: 0,
      loading: false,
      totalPatients: 0,
      currentGroupId: null,
      patientList: []
    }
  },
  created() {
    this.getGroupList()
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    async getGroupList() {
      try {
        this.loading = true;
        const response = await selectGroup();
        if (response.code === 200) {
          this.groupList = response.data || [];
          this.totalPatients = this.groupList.reduce((total, group) => {
            return total + (group.patientList ? group.patientList.length : 0);
          }, 0);
        } else {
          this.$message.error(response.msg || '获取分组失败');
        }
      } catch (error) {
        console.error('获取分组失败:', error);
        this.$message.error('获取分组失败，请稍后重试');
      } finally {
        this.loading = false;
      }
    },
    async selectPatients(groupId) {
      // 直接跳转到患者选择页面，传递分组ID
      this.$router.push({
        path: 'selectPatients',
        query: {
          id: groupId  // 直接传递分组id，与后端接口参数对应
        }
      });
    },
    handleSearch() {
      // 如果当前有选中的分组，则根据关键字搜索该分组下的患者
      if (this.currentGroupId) {
        this.selectPatients(this.currentGroupId);
      }
    },
    confirmSelection() {
      // 确认选择，返回到新增消息页面
      this.$router.push({
        path: '/message/add',
        query: {
          selectedPatients: JSON.stringify(this.selectedPatients)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.select-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.nav-header {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid #eee;

  .back-btn {
    font-size: 20px;
    color: #333;
    margin-right: 16px;
    cursor: pointer;
  }

  .title {
    font-size: 18px;
    font-weight: 500;
  }
}

.search-box {
  padding: 12px 16px;
  background-color: #fff;

  .search-input {
    width: 100%;
    height: 36px;
    background-color: #f5f5f5;
    border: none;
    border-radius: 18px;
    padding: 0 16px;
    font-size: 14px;

    &::placeholder {
      color: #999;
    }
  }
}

.disease-list {
  .group-item {
    background-color: #fff;
    margin-bottom: 1px;

    .group-header {
      display: flex;
      align-items: center;
      padding: 16px;
      cursor: pointer;

      &:active {
        background-color: #f5f5f5;
      }

      .group-name {
        flex: 1;
        font-size: 14px;
        color: #333;
      }

      .patient-count {
        font-size: 14px;
        color: #999;
        margin-right: 8px;
      }

      .arrow-right {
        color: #999;
      }
    }
  }
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background-color: #fff;
  border-top: 1px solid #eee;

  .selected-count {
    font-size: 14px;
    color: #666;
  }

  .confirm-btn {
    padding: 8px 24px;
    background-color: #409eff;
    color: #fff;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;

    &:active {
      background-color: #3a8ee6;
    }
  }
}
</style>
